<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <counter v-on:la='ha($event)'></counter>
        <div>子组件计算出来的结果是: {{addTotal}} </div>
    </div>
    <script type="text/javascript" src="js/vue.js"></script>
    <script type="text/javascript">
        Vue.component('counter', {
            data: function() {
                return {
                    num1: 0,
                    num2: 0,
                    result: 0
                }
            },
            methods: {
                add: function() {
                    this.result = parseInt(this.num1) + parseInt(this.num2)
                    this.$emit('la', this.result)
                }
            },
            template: `
      <div>
        <input type="text" v-model="num1">
        +
        <input type="text" v-model="num2">
        <button @click="add">=</button>
        <span>{{result}}</span>
        <br/>
        <span>---------------------------------------------------------------</span>
      </div>
      `
        });
        var vm = new Vue({
            el: '#app',
            data: {
                addTotal: 0
            },
            methods: {
                ha: function(i) {
                    this.addTotal = i
                }
            }
        });
        /**
          将子组件counter计算出来的值 result 传递给vm对象中的addTotal数据, 并在#app展示出来
        **/
    </script>
</body>

</html>